<?php

/*
 * 访客管理
 */

namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class VisitorController extends Controller{

    //全部访客
    public function AllVisitor(Request $request,$type){
        if($request ->isMethod('post')){
            $village_id        = $request ->input('village_id');
            $time      = date('Y-m-d H:i:s',time());
            if($type ==2){
               // $time      = date('Y-m-d H:i:s',time());
                $data = DB::table('user')
                    ->select('visitors_id', 'visitors_name', 'visitors_create_time')
                    ->leftjoin('visitors', 'user.user_id', '=', 'visitors.user_visitors')
                    ->where('user_village', $village_id)
                    ->where('visitors_time','<',$time )
                    ->get();
                if ($data == true) {
                    return json::json('success', '获取全部访客成功', $data);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }else {
                $data = DB::table('user')
                    ->select('visitors_id', 'visitors_name', 'visitors_create_time')
                    ->leftjoin('visitors', 'user.user_id', '=', 'visitors.user_visitors')
                    ->where('user_village', $village_id)
                    ->where('visitors_time','>',$time )
                    ->where('visitors_state', $type)
                    ->get();
                if ($data == true) {
                    return json::json('success', '获取全部访客成功', $data);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    //获取访客详情
    public function SelectVisitor(Request $request){
        if($request ->isMethod('post')){
            $visitors_id      = $request ->input('visitors_id');
            $data             = DB::table('visitors')
                              ->leftjoin('user','visitors.user_visitors','=','user.user_id')
                              ->leftjoin('village','user.user_village','=','village.village_id')
                              ->where('visitors_id',$visitors_id)
                              ->get();
            foreach ($data as $value) {
                $nickname = $value->nickname;
                $phone = $value->phone;
                $visitors_name = $value->visitors_name;
                $visitors_sex = $value->visitors_sex;
                $user_roomname = $value ->user_roomname;
                $address      = $value->village_name.'-'.$user_roomname;
                $drive = $value->drive;
                $license = $value->license;
                $number = $value->number;
                $visitors_state = $value->visitors_state;
                $visitors_time = $value->visitors_time;
                $time = $value->visitors_create_time;
                $da = array('nickname' => $nickname,'address'=>$address, 'phone' => $phone,'visitors_name'=>$visitors_name,'visitors_sex'=>$visitors_sex,
                   'drive'=>$drive,'license'=>$license,'number'=>$number,'visitors_state'=>$visitors_state,'visitors_create_time'=>$time,
                    'time'=>$visitors_time);
            }
                if ($data == true) {
                    return json::json('success', '获取访客详情成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }else{
                return json::json('101','提交方式错误','');
            }
        }

        //扫码进入
    public function ScanCode(Request $request){
        if($request ->isMethod('post')){
            $user_id         = $request ->input('visitors_id');
            $toke            = $request ->input('toke');
            $data            = DB::table('visitors')
                             ->where('visitors_id',$user_id)
                             ->where('visitors_toke',$toke)
                             ->update([
                                 'visitors_state' =>1
                             ]);
            if($data == true){
                return json::json('success', '欢迎来到本小区', '');
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }

        }

    }


}